<Project>
  <PropertyGroup>
    <_targetFrameworkIdentifier Condition="'$(TargetFrameworkIdentifier)' != ''">$(TargetFrameworkIdentifier.Substring(1).ToLower())</_targetFrameworkIdentifier>
    <!-- Make sure the SDK raises the runtime items so that they are passed to conflict resolution -->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <CopyLocalRuntimeTargetAssets>true</CopyLocalRuntimeTargetAssets>
    <!-- Suppress any SYSLIB9000 errors, as in these cases restore/build would succeed, the failure would be at run-time -->
    <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
  </PropertyGroup>

  <ItemGroup>
    <!-- Required for VerifyClosure and VerifyTypes tasks. -->
    <PackageReference Include="Microsoft.DotNet.PackageTesting" Version="$(MicrosoftDotNetPackageTestingVersion)" />
  </ItemGroup>

  <!-- Import framework settings. -->
  <Import Project="frameworkSettings\$(_targetFrameworkIdentifier)\*.targets"
          Condition="'$(_targetFrameworkIdentifier)' != ''"  />
  <Import Project="frameworkSettings\$(TargetFramework)\*.targets" />
  <!-- Import package settings. -->
  <Import Project="packageSettings\$(TestPackageId)\*.targets" />
  <Import Project="packageSettings\$(TestPackageId)\$(_targetFrameworkIdentifier)\*.targets"
          Condition="'$(_targetFrameworkIdentifier)' != ''" />
  <Import Project="packageSettings\$(TestPackageId)\$(TargetFramework)\*.targets"
          Condition="'$(TargetFramework)' != ''" />

  <ItemGroup>
    <!-- Type duplicated: https://github.com/dotnet/runtime/issues/33998 -->
    <IgnoredTypes Include="Microsoft.Extensions.Logging.LoggingBuilderExtensions" />
    <!-- Type duplicated: https://github.com/dotnet/runtime/issues/34420 -->
    <IgnoredTypes Include="System.Collections.Generic.CollectionExtensions" />
  </ItemGroup>

  <Target Name="LogBeginTest"
          Condition="'$(TargetFramework)' != ''">
    <Message Importance="High" Text="Testing $(TestPackageID) TFM=$(TargetFramework)" />
  </Target>

  <Target Name="VerifyReferenceClosure"
          DependsOnTargets="ResolveReferences"
          Condition="'$(ShouldVerifyClosure)' == 'true'">
    <ItemGroup>
      <_refClosureFileNames Include="@(ReferencePath->'%(FileName)')">
        <Original>%(Identity)</Original>
      </_refClosureFileNames>
      <_refClosureFileNamesFiltered Include="@(_refClosureFileNames)" Exclude="@(ExcludeFromClosure);@(ExcludeFromClosure->'%(Identity).ni')"/>
      <_refClosureFileFiltered Include="@(_refClosureFileNamesFiltered->'%(Original)')"/>
    </ItemGroup>

    <VerifyClosure Condition="'@(_refClosureFileFiltered)' != ''"
                   Sources="@(_refClosureFileFiltered)"
                   IgnoredReferences="@(IgnoredReference)" />
  </Target>

  <Target Name="VerifyReferenceTypes"
          DependsOnTargets="ResolveReferences"
          Condition="'$(ShouldVerifyTypes)' == 'true'">
    <ItemGroup>
      <_refTypesFileNames Include="@(ReferencePath->'%(FileName)')">
        <Original>%(Identity)</Original>
      </_refTypesFileNames>
      <_refTypesFileNamesFiltered Include="@(_refTypesFileNames)" Exclude="@(ExcludeFromTypes);@(ExcludeFromTypes->'%(Identity).ni')"/>
      <_refTypesFileFiltered Include="@(_refTypesFileNamesFiltered->'%(Original)')"/>
    </ItemGroup>

    <VerifyTypes Condition="'@(_refTypesFileFiltered)' != ''"
                 Sources="@(_refTypesFileFiltered)"
                 IgnoredTypes="@(IgnoredTypes)" />
  </Target>

  <Target Name="PrepareForRuntimeTesting" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <ReferenceLibToTest Include="@(ReferencePath)" Condition="'%(ReferencePath.NuGetPackageId)' == '$(TestPackageId)'" />
      <RuntimeLibToTest Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == '$(TestPackageId)'" />
      <!-- Don't verify RID specific libs that aren't compatible on .NETFramework, i.e. Unix. -->
      <RuntimeLibToTest Remove="@(RuntimeLibToTest)" Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and
                                                                '%(RuntimeLibToTest.RuntimeIdentifier)' != '' and
                                                                '%(RuntimeLibToTest.RuntimeIdentifier)' != 'win' and
                                                                !$([System.String]::Copy('%(RuntimeLibToTest.RuntimeIdentifier)').StartsWith('win-'))" />
      <RuntimeLibToTest Condition="'%(RuntimeLibToTest.RuntimeIdentifier)' == ''" RuntimeIdentifier="none" />
    </ItemGroup>

    <ItemGroup>
      <RuntimeLibToTestDependency Include="@(ReferencePath)" Condition="'%(ReferencePath.NuGetPackageId)' != '$(TestPackageId)'" />

      <!-- Some dependent packages may be excluded from compile, consider these as candidates as well.-->
      <ReferenceCopyLocalPaths Original="%(Identity)" />
      <_referenceCopyLocalPathsPackages Include="@(ReferenceCopyLocalPaths->'%(NuGetPackageId)')"
                                        Condition="'%(ReferenceCopyLocalPaths.RuntimeIdentifier)' == ''"
                                        Exclude="@(ReferencePath->'%(NuGetPackageId)');$(TestPackageId)" />
      <RuntimeLibToTestDependency Include="@(_referenceCopyLocalPathsPackages->'%(Original)')" />
    </ItemGroup>

    <Error Condition="'@(RuntimeLibToTest)' == '' AND '@(ReferenceLibToTest)' != ''" Text="Could not locate any runtime items from Package $(TestPackageID)" />

    <Message Condition="'@(RuntimeLibToTest)' != ''" Importance="High" Text="Testing $(TestPackageID) runtime on TFM=$(TargetFramework) RIDs=@(RuntimeLibToTest->'%(RuntimeIdentifier)'->Distinct())" />
  </Target>

  <Target Name="VerifyRuntimeClosure"
          Inputs="%(RuntimeLibToTest.RuntimeIdentifier)"
          Outputs="unused"
          DependsOnTargets="PrepareForRuntimeTesting"
          Condition="'$(ShouldVerifyClosure)' == 'true' and '$(SkipVerifyClosureForRuntime)' != 'true' and '$(SkipVerifyRuntime)' != 'true'">
    <Message Importance="High" Text="Testing closure for $(TestPackageID) TFM=$(TargetFramework) RID=%(RuntimeLibToTest.RuntimeIdentifier)" />
    <ItemGroup>
      <_runClosureFileNames Include="@(RuntimeLibToTestDependency->'%(FileName)');@(RuntimeLibToTest->'%(FileName)')">
        <Original>%(Identity)</Original>
      </_runClosureFileNames>
      <_runClosureFileNamesFiltered Include="@(_runClosureFileNames)" Exclude="@(ExcludeFromClosure);@(ExcludeFromClosure->'%(Identity).ni')"/>
      <_runClosureFileFiltered Include="@(_runClosureFileNamesFiltered->'%(Original)')"/>
    </ItemGroup>

    <VerifyClosure Condition="'@(_runClosureFileFiltered)' != ''"
                   Sources="@(_runClosureFileFiltered)"
                   IgnoredReferences="@(IgnoredReference)" />
  </Target>

  <Target Name="VerifyRuntimeTypes"
          Inputs="%(RuntimeLibToTest.RuntimeIdentifier)"
          Outputs="unused"
          DependsOnTargets="PrepareForRuntimeTesting"
          Condition="'$(ShouldVerifyTypes)' == 'true' and '$(SkipVerifyRuntime)' != 'true'">
    <Message Importance="High" Text="Testing for duplicate types for $(TestPackageID) TFM=$(TargetFramework) RID=%(RuntimeLibToTest.RuntimeIdentifier)" />
    <ItemGroup>
      <_runTypesFileNames Include="@(RuntimeLibToTestDependency->'%(FileName)');@(RuntimeLibToTest->'%(FileName)')">
        <Original>%(Identity)</Original>
      </_runTypesFileNames>
      <_runTypesFileNamesFiltered Include="@(_runTypesFileNames)" Exclude="@(ExcludeFromTypes);@(ExcludeFromTypes->'%(Identity).ni')"/>
      <_runTypesFileFiltered Include="@(_runTypesFileNamesFiltered->'%(Original)')"/>
    </ItemGroup>

    <VerifyTypes Condition="'@(_runTypesFileFiltered)' != ''"
                 Sources="@(_runTypesFileFiltered)"
                 IgnoredTypes="@(IgnoredTypes)" />
  </Target>

  <Target Name="VerifyNotDependsOnNetStandard"
          DependsOnTargets="ResolveReferences"
          Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and
                      '$(DisableVerifyNotDependsOnNetStandardTest)' != 'true'">
    <Error Condition="'$(DependsOnNetStandard)' == 'true'" Text="Package $(TestPackageId) requires netstandard shims when targeting $(TargetFramework)" />
  </Target>

  <PropertyGroup>
    <TestDependsOn>
      LogBeginTest;
      VerifyNotDependsOnNetStandard;
      VerifyReferenceClosure;
      VerifyReferenceTypes;
      VerifyRuntimeClosure;
      VerifyRuntimeTypes;
    </TestDependsOn>
    <InnerTargets>Test</InnerTargets>
  </PropertyGroup>

  <!-- Runs all tests scenarios for this project -->
  <Target Name="Test" DependsOnTargets="$(TestDependsOn)" />
</Project>
